package com.ikkong.shine.controller;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.shine.model.Doctor;
import com.ikkong.shine.model.Patient_code;
import com.ikkong.shine.model.Patient_queue;
import com.ikkong.shine.model.QueueType;
import com.ikkong.shine.service.DoctorService;
import com.ikkong.shine.service.Patient_codeService;
import com.ikkong.shine.service.Patient_queueService;
import com.ikkong.shine.service.QueueTypeService;
import com.ikkong.shine.service.impl.DoctorServiceImpl;
import com.ikkong.shine.service.impl.Patient_codeServiceImpl;
import com.ikkong.shine.service.impl.Patient_queueServiceImpl;
import com.ikkong.shine.service.impl.QueueTypeServiceImpl;
import com.ikkong.system.controller.base.UrlPermissController;
import com.jfinal.kit.StrKit;

/**
 * Generated by Blade.
 * 2017-03-24 14:38:29
 */
public class Patient_queueController extends UrlPermissController {
	private static String CODE = "patient_queue";
	private static String PERFIX = "patient_queue";
	private static String LIST_SOURCE = "Patient_queue.list";
	private static String BASE_PATH = "/platform/patient_queue/";
	
	Patient_queueService service = new Patient_queueServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "patient_queue.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "patient_queue_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Patient_queue patient_queue = service.findById(id);
		setAttr("model", JsonKit.toJson(patient_queue));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "patient_queue_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Patient_queue patient_queue = service.findById(id);
		setAttr("model", JsonKit.toJson(patient_queue));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "patient_queue_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Patient_queue patient_queue = mapping(PERFIX, Patient_queue.class);
		boolean temp = service.save(patient_queue);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Patient_queue patient_queue = mapping(PERFIX, Patient_queue.class);
		//patient_queue.setVersion(patient_queue.getVersion()+1);
		boolean temp = service.update(patient_queue);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	/*
	患者编号		必要	patient_id	Varchar(50)	NOT NULL	患者唯一编号
	病人姓名		必要	patient_name	Varchar(20)	NOT NULL	
	队列类型编号		必要	queue_type_source_id	Varchar (100)	NOT NULL	对应Queue_type排队队列视图中source_id字段
	数据源扫描条码			source_code	Varchar(200)		可设置多个扫描条码，磁条卡号码等
							
				注：多种条码时可自定义字段名称			
				例如：			
				source_code1			
				source_code2			
	挂号单号或检查单号			register_id	varchar(20)	NOT NULL	显示患者编号用，如有特殊需求可对此项定制。例如“+1号”；取排队队列的序号或科室排队队列的序号（挂号条上的队列排队序号）
	队列编号			queue_num	Double	NOT NULL	每种队列的排队顺序，默认与register_id(数据为整数的情况下)相同，如需按照挂号时间先后排序，可将此项值设置为0
	队列级别			sub_queue_order	Tinyint(4)	NOT NULL	如队列有多种优先组合时使用。叫号优先级，默认情况下固定值为0，如有特殊群体需叫号优先请取值1或更大数字。数字越大优先级越高。
	队列级别名称			sub_queue_type	Varchar(20)	NOT NULL	不同叫号优先级队列级别的显示名称：军人、预约、初诊等
	午别			time_interval	Tinyint(4)	0,1,2	0,全天
							1,上午
							2,下午
	医生编号			doctor_source_id	Varchar (100)		挂号或开检查单时指定医生时使用
							对应视图Doctor中的Login_id
	退号标识			is_deleted	Tinyint	0,1	退号，退费等数据对比删除处理；如不存在此项处理则固定0 as is_deleted
							1，  需删除；
							0，正常
	时间戳			fre_date	Datetime	NOT NULL	挂号时间、登记时间、取号时间等
	扩展字段			Content	text		如有特殊显示需求，可将此字段进行扩展*/

	public void save_patient(){
		Map<String, Object> map=new HashMap<String, Object>();
		String patient_strs= getPara("patients");
		if(StrKit.isBlank(patient_strs)){
			map.put("code", -1);
			map.put("msg", "请传值");
			renderJson(map);
			return;
		}else{
			JSONArray patientList= JSONArray.parseArray(patient_strs);
			if(patientList !=null){
				Patient_queue pq= null;
				for (Object json : patientList) {
					JSONObject patient_json = (JSONObject) json;
					pq=new Patient_queue();
					QueueTypeService qt=new QueueTypeServiceImpl();
					QueueTypeService qts=new QueueTypeServiceImpl();
					
					
					DoctorService ds=new DoctorServiceImpl();
					Doctor d= ds.findByLogin_id(patient_json.getString("doctor_source_id"));
					if(null!=d){
						pq.setDoctor_id(d.getDoctor_id());
					}
					pq.setPatient_id(patient_json.getString("patient_id"));
					pq.setPatient_name(patient_json.getString("patient_name"));
					pq.setQueue_type_source_id(patient_json.getString("queue_type_source_id"));
					QueueType q= qts.findBySource_id(patient_json.getString("queue_type_source_id"));
					int queue_type_id=q.getQueue_type_id();
					pq.setQueue_type_id(queue_type_id);
					pq.setRegister_id(patient_json.getString("register_id"));
					pq.setQueue_num(patient_json.getString("queue_num"));
					pq.setSub_queue_order(patient_json.getInteger("sub_queue_order"));
					pq.setSub_queue_type(patient_json.getString("sub_queue_type"));
					pq.setTime_interval(patient_json.getInteger("time_interval"));
					//根据队列id查询该队列是否属于报道队列 
					
					QueueType queuetype= qt.findBySource_id(patient_json.getString("queue_type_source_id"));
					if("1".equals(queuetype.getIs_checkin().toString())){
						pq.setIs_display(1);//队列为报道，不显示
					}else if("2".equals(queuetype.getIs_checkin().toString())){
						pq.setIs_display(2);//队列为不报道，显示
					}
					//pq.setDoctor_id(doctor_id);
					pq.setIs_deleted(patient_json.getInteger("is_deleted"));
					pq.setFre_date(patient_json.getDate("fre_date"));
					pq.setContent(patient_json.getString("Content"));
					pq.setState_patient(0);
					boolean bool=false;
					try {
						bool=service.save(pq);
					} catch (Exception e) {
						map.put("code", -1);
						map.put("msg", "保存失败,患者编号："+patient_json.getString("patient_id")+"不是唯一值");
						renderJson(map);
						return;
					}
					if(bool){
							map.put("code", 200);
							map.put("msg", "保存成功");
							renderJson(map);
							Patient_code pc=new Patient_code();
							pc.setCode(patient_json.getString("source_code"));
							Patient_queue qp= service.findByPatient_id(patient_json.getString("patient_id"));
							int patient_queue_id=qp.getId();
							pc.setPatient_queue_id(patient_queue_id);
							Patient_codeService pcser=new Patient_codeServiceImpl();
							pcser.save(pc);
						}else{
							map.put("code", -1);
							map.put("msg", "保存失败");
							renderJson(map);
							return;
					}
				}
			}
		}
		/*String patient_id=getPara("patient_id");
		if(ToolString.isBlank(patient_id)){
			map.put("code", -1);
			map.put("msg", "请传入患者编号");
			renderJson(map);
			return;
		}
		String patient_name=getPara("patient_name");
		if(ToolString.isBlank(patient_name)){
			map.put("code", -1);
			map.put("msg", "请传入病人姓名");
			renderJson(map);
			return;
		}
		String queue_type_source_id=getPara("queue_type_source_id");
		if(ToolString.isBlank(queue_type_source_id)){
			map.put("code", -1);
			map.put("msg", "请传入队列类型编号");
			renderJson(map);
			return;
		}
		QueueTypeService qts=new QueueTypeServiceImpl();
		QueueType q= qts.findBySource_id(queue_type_source_id);
		if(null==q){
			map.put("code", -1);
			map.put("msg", "请传入正确的列类型编号");
			renderJson(map);
			return;
		}
		int queue_type_id=q.getQueue_type_id();
		String source_code=getPara("source_code");
		if(ToolString.isBlank(source_code)){
			map.put("code", -1);
			map.put("msg", "请传入患者卡号");
			renderJson(map);
			return;
		}
		String register_id=getPara("register_id");
		if(ToolString.isBlank(register_id)){
			map.put("code", -1);
			map.put("msg", "请传入挂号单号或检查单号");
			renderJson(map);
			return;
		}
		String queue_num=getPara("queue_num");
		if(ToolString.isBlank(queue_num)){
			map.put("code", -1);
			map.put("msg", "请传入队列编号");
			renderJson(map);
			return;
		}
		Integer sub_queue_order=getParaToInt("sub_queue_order");
		if("".equals(sub_queue_order)){
			map.put("code", -1);
			map.put("msg", "请传入队列级别");
			renderJson(map);
			return;
		}
		String sub_queue_type=getPara("sub_queue_type");
		if(ToolString.isBlank(sub_queue_type)){
			map.put("code", -1);
			map.put("msg", "请传入队列级别名称");
			renderJson(map);
			return;
		}
		Integer time_interval=getParaToInt("time_interval");
		String doctor_source_id=getPara("doctor_source_id");
		if(ToolString.isBlank(doctor_source_id)){
			map.put("code", -1);
			map.put("msg", "请传入医生编号");
			renderJson(map);
			return;
		}
		DoctorService ds=new DoctorServiceImpl();
		Doctor d= ds.findByLogin_id(doctor_source_id);
		Integer doctor_id=d.getDoctor_id();
		
		Integer is_deleted=getParaToInt("is_deleted");
		if(null==is_deleted){
			is_deleted=0;
		}
		Date fre_date=getParaToDate("fre_date");
		if(null==fre_date){
			map.put("code", -1);
			map.put("msg", "请传入挂号时间、登记时间、取号时间等");
			renderJson(map);
			return;
		}
		String Content=getPara("Content");
		
		Patient_queue pq=new Patient_queue();
		pq.setPatient_id(patient_id);
		pq.setPatient_name(patient_name);
		pq.setQueue_type_source_id(queue_type_source_id);
		pq.setQueue_type_id(queue_type_id);
		pq.setRegister_id(register_id);
		pq.setQueue_num(queue_num);
		pq.setSub_queue_order(sub_queue_order);
		pq.setSub_queue_type(sub_queue_type);
		pq.setTime_interval(time_interval);
		//根据队列id查询该队列是否属于报道队列
		QueueTypeService qt=new QueueTypeServiceImpl();
		QueueType queuetype= qt.findBySource_id(queue_type_source_id);
		if("1".equals(queuetype.getIs_checkin().toString())){
			pq.setIs_display(1);//队列为报道，不显示
		}else if("2".equals(queuetype.getIs_checkin().toString())){
			pq.setIs_display(2);//队列为不报道，显示
		}
		pq.setDoctor_id(doctor_id);
		pq.setIs_deleted(is_deleted);
		pq.setFre_date(fre_date);
		pq.setContent(Content);
		pq.setState_patient(0);
		boolean bool=false;
		try {
			bool=service.save(pq);
		} catch (Exception e) {
			map.put("code", -1);
			map.put("msg", "保存失败,患者编号："+patient_id+"不是唯一值");
			renderJson(map);
			return;
		}
			if(bool){
				map.put("code", 200);
				map.put("msg", "保存成功");
				renderJson(map);
				Patient_code pc=new Patient_code();
				pc.setCode(source_code);
				Patient_queue qp= service.findByPatient_id(patient_id);
				int patient_queue_id=qp.getId();
				pc.setPatient_queue_id(patient_queue_id);
				Patient_codeService pcser=new Patient_codeServiceImpl();
				pcser.save(pc);
			}else{
				map.put("code", -1);
				map.put("msg", "保存失败");
				renderJson(map);
				return;
			}*/
		renderJson(map);
	}
}
